package com.develop.skwen.basemodule.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorRes;
import android.support.annotation.DimenRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.StringRes;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.develop.skwen.basemodule.presenter.BasePresenter;

import static android.view.View.NO_ID;

/**
 * Created by skwen
 * on 18-5-29
 * 基类View，所有的布局view都继承此类并实现相应的Vu接口
 */

public abstract class BaseView<P extends BasePresenter> implements BaseVu<P> {


    private Activity mActivity;

    private P mPresenter;

    private View mView;


    @Override
    public void setActivity(Activity activity) {
        this.mActivity = activity;
    }

    @Override
    public void setPresenter(P presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public void init(LayoutInflater layoutInflater, ViewGroup viewGroup) {
        mView = layoutInflater.inflate(getLayoutRes(),viewGroup,false);
        initViews();
    }

    @Override
    public View getView() {
        return mView;
    }

    protected abstract @LayoutRes int getLayoutRes();

    protected abstract void initViews();

    protected Activity getActivity(){
        return mActivity;
    }

    protected Context getContext(){
        return mActivity;
    }

    protected P getPresenter(){
        return mPresenter;
    }

    protected final <T extends View> T findViewById(@IdRes int id){
        if (id == NO_ID){
            return null;
        }
        return mView.findViewById(id);
    }

    protected Resources getResources(){
        return mActivity.getResources();
    }

    protected String getStr(@StringRes int id){
        return getResources().getString(id);
    }

    protected int getClolorRes(@ColorRes int id){
        return getResources().getColor(id);
    }

    protected Drawable getDrawableRes(@DrawableRes int id){
        return getResources().getDrawable(id);
    }

    /**
     *
     * @param id
     * @return 获取相关像素
     */
    protected int getDimensRes(@DimenRes int id){
        return getResources().getDimensionPixelSize(id);
    }

    public void setMargins (View v, int l, int t, int r, int b) {
        if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
            p.setMargins(l, t, r, b);
            v.requestLayout();
        }
    }
}
